function Forum(){
	this.white_domain = ['event.m.shihou.tv', 'resource.youxiduo.com'];//生产环境白名单
	this.gift_id_array = [1088, 1087];//1087黑板擦、1088粉笔
	//论坛相关
	this.pageSize = 10;
	this.pageNum = 1;
	this.auditStatus = 2;//1-审核中，2-审核通过
	this.enable = 1;//0-不启用，1-启用
	this.orderType = 1;//1-时间，2-点赞数量
	this.id = null;
	this.uid = null;	//当前用户id
	this.activity_id = null;
	this.toUid = null;
	this.content = null;//发表评论的内容
	this.sentGift = false;//是否发送过礼物的状态， true发过，false没有发过
	this.init();
}
Forum.prototype={
	init: function(){
		//根据host判断当前是测试环境还是正式环境
		if(this.checkDomain()){
			this.realBaseUrl();
		}else{
			this.testBaseUrl();
		}
	},
	testBaseUrl: function(){
		console.log('测试环境');
		this.forum_base_url = 'http://112.124.121.34:8091/shihou.forum.service';
		this.forum_list_url = this.forum_base_url + '/forum/query';
		this.forum_fabulous = this.forum_base_url + '/forum/fabulous';
		this.publish_forum = this.forum_base_url + '/forum/add';
		this.check_gift = 'http://test.api.shihou.tv/api/gift/check_sent';//?user_id=1&gift_id=10047
		this.user_batch = 'http://api.shihou.tv/api/user/batch?';
		this.test_check_send_gift = true;
	},
	realBaseUrl: function(){
		console.log('生产环境');
		this.forum_base_url = 'http://event.m.shihou.tv/shihou.forum.service';
		this.forum_list_url = this.forum_base_url + '/forum/query';
		this.forum_fabulous = this.forum_base_url + '/forum/fabulous';
		this.publish_forum = this.forum_base_url + '/forum/add';
		this.check_gift = 'http://api.shihou.tv/api/gift/check_sent';//?user_id=1&gift_id=10047
		this.user_batch = 'http://api.shihou.tv/api/user/batch?';
		this.test_check_send_gift = false;
	},
	//验证domain
	checkDomain: function(){
		var domain = document.domain;
		for (var i = 0;i < this.white_domain.length;i++) {
			var domai = this.white_domain[i];
			if(domain == domai)
				return true;
		}
		return false;
	},
	//论坛列表
	getFormList: function(url){
		var _this = this;
		var data = {"pageSize":_this.pageSize, "pageNum":_this.pageNum, "auditStatus":_this.auditStatus, "enable":_this.enable, "orderType":_this.orderType, "from_user_id": _this.uid, "activityId":_this.activity_id};
		_this.logParameter(url, data);
		_this.requestRemoteDataJson(url, data, function(res){
			if(res.errorCode == "0"){
				var list = res.result.list;
				var html = '';
				var item_user_ids = '';
				for (var i=0;i < list.length;i++) {
					var zanImg = 'zan';
					var item = list[i];
					var curr_uid = _this.parseResult(item, 'uid');
					var create_time = _this.parseResult(item, 'create_time');
					var create_time1 = create_time.split(' ')[0];
					var create_time1_array = create_time1.split('-');
					create_time1 = create_time1_array[1]+'-'+create_time1_array[2];
					var create_time2 = create_time.split(' ')[1];
					var create_time2_array = create_time2.split(':');
					create_time2 = create_time2_array[0]+':'+create_time2_array[1];
					create_time = create_time1+' '+create_time2;
					if(_this.parseResult(item, 'fabulousStatus'))
						zanImg = 'yizan';
					html += '<li data-id="'+_this.parseResult(item, 'id')+'" data-uid="'+curr_uid+'">'+
		                    '    <p class="listTitle"><span>'+_this.parseResult(item, 'content')+'</span><a data-id="'+_this.parseResult(item, 'id')+'" data-uid="'+curr_uid+'" onclick="fabulous(this);"><img src="static/img/'+zanImg+'.png"/></a></p>'+
		                    '    <p class="listFont">'+
		                    '        <a id="img-'+curr_uid+'" data-create-time="'+create_time+'"></a>'+
		                    '        <span>赞：<em>'+_this.parseResult(item, 'fabulous_count')+'</em></span>'+
		                    '    </p>'+
		                    '</li>';
					item_user_ids += '&user_ids[]='+curr_uid;
				}
				if(_this.pageNum == 1)
					$('.list').html(html);
				else
					$('.list').append(html);
					
				//根据用户id取到用户信息并赋值到页面
				_this.requestRemoteDataJson(_this.user_batch+item_user_ids, {}, function(userResult){
					if(userResult.error == 0){
						var users = _this.parseResult(userResult, 'data', 'users');
						for(var i = 0;i < users.length;i++){
							var user = users[i];
							var user_id = _this.parseResult(user, 'id');
							var nick_name = _this.parseResult(user, 'nick_name');
							var avatar = _this.parseResult(user, 'avatar');
							avatar = avatar.replace("width=400", "width=100");
							avatar = avatar.replace("height=400", "height=100");
							var createTimeHtml = $('#img-'+user_id).attr('data-create-time'); 
							$('#img-'+user_id).html('<em><img src="'+avatar+'"/></em><i>'+nick_name+'</i>'+createTimeHtml);
						}
					}
				})
				
			}
		}, true);
	},
	getFormListCallback: function(url, callback){
		var _this = this;
		var data = {"pageSize":_this.pageSize, "pageNum":_this.pageNum, "auditStatus":_this.auditStatus, "enable":_this.enable, "orderType":_this.orderType, "from_user_id": _this.uid, "activityId":_this.activity_id};
		_this.logParameter(url, data);
		_this.requestRemoteDataJson(url, data, callback, false);
	},
	//发表评论
	pulibshComment: function(httpUrl, checkGiftUrl){
		var _this = this;
		var content = $("#content").val();
		if(content.length < 1){
			$(".pop").html("请填写评论内容");
			$(".pop").fadeIn(200);
			setTimeout('$(".pop").hide();', 2000);
			return;
		}
		if(!_this.sentGift){
			$(".pop").html("送过礼物才能进行评论哦!");
			$(".pop").fadeIn(200);
			setTimeout('$(".pop").hide();', 2000);
			return;
		}
		var data = {"content":_this.content, "uid": _this.uid, "activityId":_this.activity_id};
		_this.requestRemotePostDataJson(httpUrl, data, function(res){
			_this.logResult(res);
			if(res.errorCode != "0" ){
				if(res.errorCode == "305" ){
					$(".pop").html("请填写评论内容");
				}else{
					$(".pop").html(res.errorDescription);
				}
			}else{
				$(".pop").html('小师妹正在审核中~');
			}
			$(".pop").fadeIn(200);
			setTimeout('$(".pop").hide();$("#content").val("");', 1500);
		}, true);
	},
	checkSendGift: function(url){
		var _this = this;
		if(_this.test_check_send_gift){
			_this.sentGift = true;
			return true;//test	
		}
		for (var i = 0;i < _this.gift_id_array.length;i ++) {
			var data = {"user_id":_this.uid, "gift_id": _this.gift_id_array[i]};
			_this.logParameter(url, data);
			_this.requestRemoteDataJson(url, data, function(res){
				_this.logResult(res);	
				if(res.error == 0 ){
					var sent = _this.parseResult(res, 'data', 'sent');
					if(sent){
						_this.sentGift = true;
						return true;
					}
				}
			}, true);
		}
		_this.sentGift = false;
		return false;
	},
	//点赞动作
	fabulous: function(url, obj){
		var _this = this;
		var data = {"id":_this.id, "uid": _this.uid, "toUid":_this.toUid, "activityId":_this.activity_id};
		_this.requestRemotePostDataJson(url, data, function(data){
			_this.logResult(data);
			if(data.errorCode == "0" ){
				$(".pop").html("点赞成功");
				$(".pop").fadeIn(200);
				setTimeout('$(".pop").hide()', 3000);
				var target = $(obj).parent().next().find('span em');
				var fabulous = parseInt(target.html());
				target.html(fabulous+1);
				$(obj).find('img').attr('src', 'static/img/yizan.png');
			}else{
				$(".pop").html(data.errorDescription);
				$(".pop").fadeIn(200);
				setTimeout('$(".pop").hide()', 3000);
			}
		}, true);
	},
	//获取url中的参数
    getUrlParam: function(name, url) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        url = url || window.location.search.substr(1).match(reg);  //匹配目标参数
        if (url != null) return unescape(url[2]); return null; //返回参数值
    },
    getUrlParams: function(){
    	var url = window.location.href;  //匹配目标参数
    	if(url != null)
    		return url.substr(url.indexOf("?"), url.length);
    },
    parseResult:function(){//解析结果， 如果不存在则返回''
		var size = arguments.length;
		if(size == 0){
			return '';
		}
		var result = arguments[0];
		for(var i = 1; i < size; i++){
			if(result == undefined){
				return '';
			}
			var argu = arguments[i];
			if(i == size){
				return argu;
			}
			result = result[argu];
		}
		return result;
	},
	requestRemoteDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		//this.logParameter(data);
		$.ajax({
	        url: url,
	        type: 'GET',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	requestRemotePostDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		$.ajax({
	        url: url,
	        type: 'POST',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	log:function(data){
		console.log(data);
	},
	logURL:function(url){
		console.log("url --> "+url);
	},
	logParameter:function(data){
		console.log("parameter --> "+JSON.stringify(data));
	},
	logParameter:function(args, data){
		console.log(args+" --> "+JSON.stringify(data));
	},
	logResult:function(result){
		console.log("result --> "+JSON.stringify(result));
	}
}
